package ae.pcfc.etks.online.common.manager.provider;

import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;

import javax.sql.DataSource;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

import org.springframework.jdbc.datasource.DataSourceUtils;

import ae.pcfc.etks.online.common.exception.SystemException;
import ae.pcfc.etks.online.common.manager.ReportManager;

public class ReportManagerImpl implements ReportManager {

	private DataSource dataSource;

	/**
	 * fillReport
	 * 
	 * @see ae.pcfc.etks.online.common.manager.ReportManager#fillReport(java.io.File, java.util.Map)
	 * 
	 */
	public JasperPrint fillReport(File reportFile,
			Map<String, Object> parameters) throws SystemException {

		JasperPrint jasperPrint = null;
		parameters.put("BaseDir", reportFile.getParentFile());

		Connection connection = null;
		try {
			connection = DataSourceUtils.getConnection(dataSource);
			jasperPrint = JasperFillManager.fillReport(reportFile.getPath(),
					parameters, connection);

		} catch (JRException e) {
			throw new SystemException(e);
		} finally {
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					throw new SystemException(e);
				}
			}
		}

		return jasperPrint;

	}

	/**
	 * setDataSource
	 * 
	 * @param dataSource
	 * void
	 */
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

}
